/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sda.levelup.circles;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.awt.image.ImageObserver.WIDTH;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Дима
 */
public class MainFrame extends JFrame
{
    
    private Toolkit t = Toolkit.getDefaultToolkit();
    private Dimension d = t.getScreenSize();
    
    private int x;
    private int y;
    private int Ox = (int) d.getWidth() / 2;
    private int Oy = (int) d.getHeight() / 2;
    private int R = 100;
    private double angle = 0;
    private boolean flag = true;
    MainPanel b;
    
    public MainFrame()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(450, 150);
        setLocation(Ox, Oy);
        fillFrame();
    }
    
    public void runMainLoop()
    {
        while (true)
        {
            try
            {
                while (flag)
                {
                    
                    x = (int) Math.round(Ox + R * Math.cos(angle));
                    y = (int) Math.round(Oy + R * Math.sin(angle));
                    setLocation(x, y);
                    repaint();
                    b.repaint();
                    angle += 0.01d;
                    try
                    {
                        Thread.sleep(200);
                    }
                    catch (Exception e)
                    {
                    }
                }
                Thread.sleep(20);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    
    public void fillFrame()
    {
        b = new MainPanel();
        add(b);
    }
    
    public void SetLoc(int x, int y)
    {
        
        setLocation(x, y);
    }
    
    class MainPanel extends JPanel
    {
        
        public MainPanel()
        {
            setLayout(new BorderLayout());
            add(new StringPanel(), BorderLayout.NORTH);
            add(createButtons(), BorderLayout.SOUTH);

            //add(createSinCos(), BorderLayout.NORTH);
        }
        
        public JPanel createButtons()
        {
            JPanel buttons = new JPanel();
            JButton left = new JButton("Step Left");
            JButton start = new JButton("Start");
            JButton stop = new JButton("Stop");
            JButton pause = new JButton("Pause");
            JButton right = new JButton("Step Right");
            stop.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    flag = false;
                    x = Ox;
                    y = Oy;
                    angle = 0;
                    MainFrame.this.setLocation(x, y);
                    MainFrame.this.repaint();
                }
            });
            pause.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    flag = false;
                    
                }
            });
            start.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    flag = true;
                }
            });
            left.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    flag = false;
                    angle += 0.01d;
                    x = (int) (Ox + R * Math.cos(angle));
                    y = (int) (Oy + R * Math.sin(angle));
                    MainFrame.this.setLocation(x, y);
                    MainFrame.this.repaint();
                }
            });
            right.addActionListener(new ActionListener()
            {
                
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    flag = false;
                    angle -= 0.01d;
                    x = (int) (Ox + R * Math.cos(angle));
                    y = (int) (Oy + R * Math.sin(angle));
                    MainFrame.this.setLocation(x, y);
                    MainFrame.this.repaint();
                }
            });
            buttons.add(left);
            buttons.add(start);
            buttons.add(stop);
            buttons.add(pause);
            buttons.add(right);
            return buttons;
        }
        
        public JPanel createSinCos()
        {
            JPanel angles = new JPanel();
            JLabel sin = new JLabel("Sin= " + Math.sin(angle));
            JLabel cos = new JLabel("Cos= " + Math.cos(angle));
            Box box = Box.createVerticalBox();
            box.add(sin);
            box.add(cos);
            angles.add(box);
            return angles;
        }
    }
    
    class StringPanel extends JPanel
    {
        
        public StringPanel()
        {
            setMinimumSize(new Dimension(450, 50));
            setMaximumSize(new Dimension(450, 50));
            setPreferredSize(new Dimension(450, 50));
        }
        
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawString("Sin = " + Math.sin(angle), 160, 10);
            g.drawString("Cos = " + Math.cos(angle), 160, 30);
        }
    }
    
}
